package com.elegantmall.controller;


import com.elegantmall.dto.SearchRequest;
import com.elegantmall.pojo.GoodsPojo;
import com.elegantmall.pojo.SellerPojo;
import com.elegantmall.service.SearchServiceImpl;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@RestController
@CrossOrigin
public class UserViewController
{
    private final SearchServiceImpl searchService;

    UserViewController(SearchServiceImpl searchService) {
        this.searchService = searchService;
    }

    @RequestMapping(value = "/userView" , method = RequestMethod.GET)
    public Object findAll(){
        return searchService.getAllGoods();
    }

    @RequestMapping(value = "/userView" , method = RequestMethod.POST)
    public Object Search(
            @RequestParam String searchType,
            @RequestParam String keyWords) {
        return switch (searchType) {
            case "product" -> searchByProduct(keyWords);
            case "store" -> searchBySeller(keyWords);
            default -> null;
        };
    }

    @RequestMapping(value = "/api/category" , method = RequestMethod.POST)
    public Object searchByCategory(
            @RequestParam String category
    ){
        return searchService.searchGoodsByCategory(category);
    }

    private List<SellerPojo> searchBySeller(String keyWords) {

        return searchService.searchSellerByName(keyWords);
    }

    private List<GoodsPojo> searchByProduct(String keyWords) {

        return searchService.searchGoodsByName(keyWords);
    }

}


